package com.net.xpay.common.service.impl.admin.rbac;

import com.net.xpay.common.domain.admin.rbac.AdminUserRole;
import com.net.xpay.common.domain.admin.rbac.RoleMenu;
import com.net.common.annotation.WriteTransactional;
import com.net.xpay.common.manager.admin.rbac.AdminUserRoleManager;
import com.net.xpay.common.manager.admin.rbac.RoleMenuManager;
import com.net.xpay.common.service.admin.rbac.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 2019/11/13 17:47

 */
@Slf4j
@Service
@WriteTransactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMenuManager roleMenuManager;

    @Autowired
    private AdminUserRoleManager adminUserRoleManager;

    @Override
    public void saveRoleMenu(Long roleId, List<Long> menuIds) {
        roleMenuManager.deleteByRoleId(roleId);
        if (CollectionUtils.isNotEmpty(menuIds)) {
            for (Long menuId : menuIds) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menuId);
                roleMenuManager.save(roleMenu);
            }
        }
    }

    @Override
    public void saveAdminUserRoles(Long adminUserId, List<Long> roleIds) {
        adminUserRoleManager.deleteByAdminUserId(adminUserId);

        if (CollectionUtils.isNotEmpty(roleIds)) {
            for (Long roleId : roleIds) {
                AdminUserRole adminUserRole = new AdminUserRole();
                adminUserRole.setRoleId(roleId);
                adminUserRole.setAdminUserId(adminUserId);
                adminUserRoleManager.save(adminUserRole);
            }
        }
    }
}
